<?php

namespace Roles\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Zend\View\Model\ViewModel;
use Roles\Model\PerfilXPermiso;
use Roles\Form\ProfileSearchForm;

class PerfilXPermisoController extends AbstractActionController {

   protected $perfilTable;
   protected $permisoTable;
   protected $perfilXpermisoTable;
   protected $usuarioTable;

   private function checkLogin() {
      if (! $this->getServiceLocator()
                 ->get('AuthService')->hasIdentity()){
         return $this->redirect()->toRoute('login');
      }
   }
   private function checkRole() {
      $username = $this->getServiceLocator()->get('AuthService')->getIdentity();
      $usuario = $this->getUsuarioTable()->getUsuarioByUserName($username);

      $path = $this->getRequest()->getUri()->getPath();
      if ($path[0] != '/') $path = '/' . $path;
      $pieces = explode("/",$path);
      $module = $pieces[1]; $action = $pieces[2];
      if ($action == "") $action = "index";

      if (! $this->getServiceLocator()
                 ->get('RoleService')->hasPermission($usuario->id,$module,$action)) {

         return $this->redirect()->toRoute('restringido');
      }
   }

   public function indexAction() {

      $this->checkLogin();
      $this->checkRole();

      $form = new ProfileSearchForm();
      $form->get('search_submit')->setValue('Buscar');
      $form->get('save_submit')->setValue('Guardar');

      $list = $this->getPerfilTable()->fetchAll();
      $select = array();
      foreach ($list as $row) {
         $select[$row->id] = $row->nombre_perfil;
      }

      $form->get('perfiles')->setValueOptions($select);

      $request = $this->getRequest();
      if ($request->isPost()) {
         $data = $request->getPost();

         if (isset($_POST['search_submit'])) {

            if ($data['perfiles'] != '') {
               $form->get('save_submit')->setAttribute("disabled", false);
               //echo $data['perfiles'];
               $id = $data['perfiles'];
               $permisos = $this->getPermisoTable()->getPermisosByPerfil($id);
            } else {
               $form->get('save_submit')->setAttribute("disabled", true);
               echo "
                     <script language='JavaScript'>
                        alert('ERROR: Seleccione un perfil!');
                     </script>
                   ";
            }

         } else if (isset($_POST['save_submit'])) {
//            var_dump($data);
            $id = $data['perfil_id']; $data['perfiles'] = $id;
            $permisos = $this->getPermisoTable()->getPermisosByPerfil($id);
            $i = 0;
            foreach ($permisos as $permiso) {
               if ($data['checkbox_' . $i] == "on" && is_null($permiso->id_perfil_permiso)) {
                  $perfilXpermiso = new PerfilXPermiso();
                  $perfilXpermiso->setIdPerfil($data['perfil_id']);
                  $perfilXpermiso->setIdPermiso($permiso->id);
                  $this->getPerfilXPermisoTable()->savePerfilXPermiso($perfilXpermiso);
               } else if ($data['checkbox_' . $i] == "off" && !is_null($permiso->id_perfil_permiso)) {
                  $this->getPerfilXPermisoTable()->deletePerfilXPermiso($permiso->id_perfil_permiso);
               }

               $i++;
            }

            $permisos = $this->getPermisoTable()->getPermisosByPerfil($id);

         }

      } else {
         $permisos =array();
         $data = array(); $data['perfiles'] = 0;
         $select[0] = '';
      }

      return array(
         'form'      => $form,
         'perfil'    => $select[$data['perfiles']],
         'perfil_id' => $data['perfiles'],
         'permisos'  => $permisos,
      );

   }

   public function getPerfilTable() {
      if (!$this->perfilTable) {
         $sm = $this->getServiceLocator();
         $this->perfilTable = $sm->get('Roles\Model\PerfilTable');
      }
      return $this->perfilTable;
   }

   public function getPermisoTable() {
      if (!$this->permisoTable) {
         $sm = $this->getServiceLocator();
         $this->permisoTable = $sm->get('Roles\Model\PermisoTable');
      }
      return $this->permisoTable;
   }

   public function getPerfilXPermisoTable() {
      if (!$this->perfilXpermisoTable) {
         $sm = $this->getServiceLocator();
         $this->perfilXpermisoTable = $sm->get('Roles\Model\PerfilXPermisoTable');
      }
      return $this->perfilXpermisoTable;
   }

   public function getUsuarioTable() {
      if (!$this->usuarioTable) {
         $sm = $this->getServiceLocator();
         $this->usuarioTable = $sm->get('Usuarios\Model\UsuarioTable');
      }
      return $this->usuarioTable;
   }

}
